local jinti = fk.CreateSkill {
  name = "wk_heg__jinti",
}

Fk:loadTranslationTable{
  ["wk_heg__jinti"] = "进替",
  [":wk_heg__jinti"] = "与你势力相同的角色于其回合内明置武将牌后，你可摸X张牌（X为与你势力相同的角色数），然后其以此法明置武将牌上的非锁定技于本回合内无效。",

  ["@wk_heg__jinti-turn"] = "进替",
}

local H = require "packages.ol_hegemony.util"

jinti:addEffect(fk.GeneralRevealed, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(jinti.name) and H.compareKingdomWith(player, target) and player.room.current == target
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = 0
    for _, p in ipairs(room.alive_players) do
      if H.compareKingdomWith(p, player) then
        n = n + 1
      end
    end
    player:drawCards(n, jinti.name)
    local generals = target:getTableMark("@wk_heg__jinti-turn")
    local skills = target:getTableMark("_wk_heg__jinti-turn")
    for _, v in pairs(data) do
      table.insertIfNeed(generals, v)
      for _, sname in ipairs(Fk.generals[v]:getSkillNameList()) do
        local skill = Fk.skills[sname]
        if not skill:hasTag(Skill.Compulsory) and skill:isPlayerSkill(target) then
          table.insertIfNeed(skills, sname)
        end
      end
    end
    room:setPlayerMark(target, "@wk_heg__jinti-turn", generals)
    room:setPlayerMark(target, "_wk_heg__jinti-turn", skills)
  end,
})

jinti:addEffect("invalidity", {
  invalidity_func = function(self, from, skill)
    if from:getMark("_wk_heg__jinti-turn") ~= 0 then
      return table.contains(from:getMark("_wk_heg__jinti-turn"), skill.name) and
          not skill:hasTag(Skill.Compulsory) and skill:isPlayerSkill(from)
    end
  end
})

return jinti